package cn.blnp.net.flowable.identity.user;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.User;
import org.flowable.idm.api.UserQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h3>用户管理API案例测试</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/4/30 14:00
 */
@Slf4j
@SpringBootTest
public class FlowUserTest {

    @Resource
    private IdentityService identityService;

    /**
     * <p><b>用途:新建用户<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 14:01 2025/4/30
     * @params [id, lastName, firstName, displayName, email, password] 
     * @param id 用户编号
     * @param lastName 用户姓氏
     * @param firstName 用户名字
     * @param displayName 用户显示名
     * @param email 用户电子邮箱
     * @param password 用户密码
     * @return void
    **/
    public void addUser(String id, String lastName, String firstName, String displayName, String email, String password) {
        //调用IdentityService的newUser()方法创建User实例
        User newUser = identityService.newUser(id);
        //调用setter()方法为创建的User实例设置属性值
        newUser.setFirstName(firstName);
        newUser.setLastName(lastName);
        newUser.setDisplayName(displayName);
        newUser.setEmail(email);
        newUser.setPassword(password);
        //调用IdentityService的saveUser()方法将User实例保存到数据库中
        identityService.saveUser(newUser);
    }

    /**
     * <p><b>用途:用户列表查询<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 14:03 2025/4/30
     * @params [userQuery] 
     * @param userQuery 
     * @return void
    **/
    public void executeList(UserQuery userQuery) {
        List<User> users = userQuery.list();
        for (User user : users) {
            log.info("用户编号：{}，姓名：{}，显示名：{}，邮箱：{}", user.getId(), user.getLastName() + user.
                    getFirstName(), user.getDisplayName(), user.getEmail());
        }
    }

    /**
     * <p><b>用途:用户查询测试<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 14:02 2025/4/30
     * @params []
     * @return void
    **/
    @Test
    public void test() {
        //创建用户
        addUser("hebo", "张", "三", "诗雨花魂", "hebo824@163.com", "");
        addUser("liuxiaopeng", "李", "晓鹏", "阿凡提", "lxpcnic@163.com", "");
        addUser("huhaiqin", "赵", "六", "清波~微醉", "aiqinhai_hu@163.com", "");
        //初始化UserQuery
        UserQuery userQuery = identityService.createUserQuery();
        //查询所有用户
        executeList(userQuery);

        /**
         *  演示结果：
         *  2025-04-30 14:05:59.753  INFO 24040 --- [           main] c.b.n.f.identity.user.FlowUserTest       : 用户编号：hebo，姓名：张三，显示名：诗雨花魂，邮箱：hebo824@163.com
         * 2025-04-30 14:05:59.753  INFO 24040 --- [           main] c.b.n.f.identity.user.FlowUserTest       : 用户编号：huhaiqin，姓名：赵六，显示名：清波~微醉，邮箱：aiqinhai_hu@163.com
         * 2025-04-30 14:05:59.753  INFO 24040 --- [           main] c.b.n.f.identity.user.FlowUserTest       : 用户编号：liuxiaopeng，姓名：李晓鹏，显示名：阿凡提，邮箱：lxpcnic@163.com
         **/
    }
}
